<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

$ids = stripslashes($param['ids']);

switch ($op) {
	case "save":
		$p = new project;
		if (isset($param['id'])) {
			$p->id = $param['id'];
			$r = $loader->get($p);
			if ($r===false) unset($p->id);
		}
		
		if (!isset($p->id)) {
			if (isset($param['parent'])) {
				$p->parent=$param['parent'];
			} else {
				$p->parent=0;
			}
		}
		
		$p->name=$param['name'];
		$path = $param['path'];
		if (substr($path,0,1)!='/') $path='/'.$path;
		if (substr($path,strlen($path)-1)=='/') $path=substr($path,0,strlen($path)-1);	
		$p->path=$path;
		if (isset($p->id)) {
			$loader->update($p);
		} else {
			$loader->save($p);	
		}

		if ($p->parent==0) {
			$tfiles = FILES.PROJECT_PREFIX.$p->id;
			$thistory = HISTORY.PROJECT_PREFIX.$p->id;
			
			if (!$db->exists($tfiles)) {
				include_once(ROOT_PATH."configs/dbdefines.php");
				$_log->debug("Creating $tfiles");
				$query = String::format($table_def['FILES']['def'],$tfiles);
				$db->query($query);
				$_log->debug("Creating $thistory");
				$query = String::format($table_def['HISTORY']['def'],$thistory);
				$db->query($query);
			}
		}
		
		success();		
		break;
	case "up": {
		$p = new project;
		$p->id = $param['id'];
		if ($loader->get($p)) {
			$p->upd = $param['value'];
			$loader->update($p);
		}
		success();
		break;
	}
	case "rm":
		$idlist = json_decode($ids);
		$_log->debug($idlist);
		
		foreach ($idlist as $uid) {
			$p = new project;
			$p->id=$uid;
			$loader->delete($p);
		}
		success();	
		break;
	case "toggle":
		$idlist = json_decode($ids);
		$_log->debug($idlist);
		
		foreach ($idlist as $uid) {
			$p = new project;
			$p->id=$uid;
			$loader->get($p);
			if ($p->status==STATUS_ACTIVE) {
				$p->status=STATUS_INACTIVE;
			} else {
				$p->status=STATUS_ACTIVE;
			}
			$loader->update($p);
		}
		success();	
		break;
	case "edit":
		$pr = new project;
		$pr->id=$param['id'];
		$loader->get($pr);	
	case "new":
		include(ROOT_PATH."/form/project_admin_form.php");
		break;
	case "subpl":
		$p = new project;
		$p->id=$param['id'];
		$list = $loader->loadListBy($p,$p->listSubProjects());
		out(jsonList($list,array("id","name","path","status","upd")));	
		break;
	case "gen":
		$idlist = json_decode($ids);
		$_log->debug($idlist);
		
		$list = array();
		$p = new project;
		foreach ($idlist as $uid) {
			$sp = new project;
			$sp->id=$uid;
			$loader->get($sp);

			if ($p->id!=$sp->parent) {
				$p->id=$sp->parent;
				$loader->get($p);
			}
			
			$t = new team;
			$nrlist = $loader->loadListBy($t,$t->listByNotReady($p->id,$uid));
			$rlist = $loader->loadListBy($t,$t->listByReady($p->id,$uid));
			
			foreach ($nrlist as $obj) {
				if ($list[$obj->id]==null) {
					$row = array('team' => $obj->name,'teamid' => $obj->id, 'status' => array());
					$list[$obj->id]=$row;
				}
			}

			foreach ($rlist as $obj) {
				if ($list[$obj->id]!=null) {
					$row = $list[$obj->id];
					array_push($row['status'],$sp->name);
					$list[$obj->id]=$row;
				} else {
					$row = array('team' => $obj->name,'teamid' => $obj->id, 'status' => array($sp->name));
					$list[$obj->id]=$row;
				}
			}
		}
		out(jsonList($list,array("teamid","team","@status")));
		break;
	case "do":
		$subprj = new project;
		$subprj->id=$param['pr'];
		$loader->get($subprj);
		
		$prj = new project;
		$prj->id=$subprj->parent;
		$loader->get($prj); 

		$team = new team;
		$team->id = $param['id'];
		$loader->get($team);
		
		$update=false;
		if ($param['type']=="update") {
			$update=true;
		}

		include(ROOT_PATH."includes/setupfilescreator.php");
		success();
		break;
	default:
		$p = new project;
		$list = $loader->loadList($p);
		out(jsonList($list,array("id","name","path","status","upd")));
		break;
}

?>